package com.zxhysy.log.annotation.advisor;

import com.zxhysy.log.annotation.LogIgnore;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * logIgnore 解析
 *
 * @author zxh
 * @since 2023/5/27 15:41:37
 */
public class LogIgnoreParser {

    /**
     * 方法参数忽略
     *
     * @return 入参忽略坐标
     */
    public static Set<Integer> methodParamsIndexIgnore(Method method) {
        Set<Integer> ignoreIndexSet = new HashSet<>();
        Annotation[][] parameterAnnotations = method.getParameterAnnotations();
        for (int i = 0; i < parameterAnnotations.length; i++) {
            for (Annotation annotation : parameterAnnotations[i]) {
                if (annotation instanceof LogIgnore) {
                    ignoreIndexSet.add(i);
                }
            }
        }
        return ignoreIndexSet;
    }

    /**
     * 获取对象需要忽略的属性
     *
     * @param obj 对象
     * @return 忽略的属性
     */
    public static Map<Class<?>, Set<String>> objIgnoreAttr(Object obj) {
        Map<Class<?>, Set<String>> objIgnoreAttr = new HashMap<>();
        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields) {
            // 是否引用ApiModelProperty注解
            boolean bool = field.isAnnotationPresent(LogIgnore.class);
            if (bool) {
                String name = field.getName();
                Set<String> ignoreName = objIgnoreAttr.getOrDefault(obj.getClass(), new HashSet<>());
                ignoreName.add(name);
                objIgnoreAttr.put(obj.getClass(), ignoreName);
            } else {
                Class<?> type = field.getType();
                if (type.getName().startsWith("com.zxhysy")) {
                    Field[] declaredFields = type.getDeclaredFields();
                    for (Field declaredField : declaredFields) {
                        boolean annotationPresent = declaredField.isAnnotationPresent(LogIgnore.class);
                        if (annotationPresent) {
                            String name = declaredField.getName();
                            Set<String> ignoreName = objIgnoreAttr.getOrDefault(type, new HashSet<>());
                            ignoreName.add(name);
                            objIgnoreAttr.put(type, ignoreName);
                        }
                    }
                }
            }
        }
        return objIgnoreAttr;
    }
    public static Set<Class<?>> getLogIgnoreClass(Object obj) {
        Set<Class<?>> objIgnoreAttr = new HashSet<>();
        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields) {
            // 是否引用ApiModelProperty注解
            boolean bool = field.isAnnotationPresent(LogIgnore.class);
            if (bool) {
                objIgnoreAttr.add(obj.getClass());
            } else {
                Class<?> type = field.getType();
                if (type.getName().startsWith("com.zxhysy")) {
                    Field[] declaredFields = type.getDeclaredFields();
                    for (Field declaredField : declaredFields) {
                        boolean annotationPresent = declaredField.isAnnotationPresent(LogIgnore.class);
                        if (annotationPresent) {
                            objIgnoreAttr.add(type);
                        }
                    }
                }
            }
        }
        return objIgnoreAttr;
    }

}
